package com.glodon.paas.document.web.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import com.glodon.paas.account.api.bean.User;
import com.glodon.paas.account.api.oauth1.AccessValidationResponse;
import com.glodon.paas.account.sdk.oauth1.web.OAuthFilter;
import com.glodon.paas.consts.StringConst;
import com.glodon.paas.document.service.util.WebService;
import com.glodon.paas.exception.PaasServerExceptionFactory;
import com.glodon.paas.exception.i18n.ErrorCode;
import com.glodon.paas.util.BeanFactoryUtil;

/**
 * @author Don Li
 */
public class SecurityUtils {

    public static User getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            return null;
        }
        if (authentication instanceof OAuth2Authentication) {
            return (User) ((OAuth2Authentication) authentication).getUserAuthentication().getDetails();
        }
        return (User) authentication.getDetails();
    }

    public static String getAuthorizerId(HttpServletRequest request) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication instanceof OAuth2Authentication) {
            if (((OAuth2Authentication) authentication).getUserAuthentication() != null) {
                return (String) ((OAuth2Authentication) authentication).getUserAuthentication().getPrincipal();
            }
            return null;
        }
        AccessValidationResponse response = OAuthFilter.getValidationResponse(request);
        return response.getUserId();
    }

    public static String getRequestUserId(HttpServletRequest request) {
        String userId = (String) request.getParameter("userId");
        if (StringUtils.isEmpty(userId)) {
            userId = (String) request.getAttribute("userId");
            if (StringUtils.isEmpty(userId)) {
                String email = (String) request.getParameter("email");
                if (StringUtils.isNotEmpty(email)) {
                    User user = ((WebService)BeanFactoryUtil.getBean("webService")).getUser(email);
                    if (user == null) {
                        throw PaasServerExceptionFactory.notFoundException("can not find User[" + email + "]", null).setErrorCode(ErrorCode.USER_NOT_EXIST);
                    } else {
                        userId = user.getId();
                        request.setAttribute("userId", userId);
                    }
                } else {
                    throw PaasServerExceptionFactory.notFoundException("can not find User", null).setErrorCode(ErrorCode.USER_NOT_EXIST);
                }
            }
        }
        return userId;
    }

    public static User getSessionUser(HttpServletRequest request) {
        User user = null;
        if (request != null && request.getSession() != null) {
            user = (User) request.getSession().getAttribute(StringConst.USER_INFO);
        }
        return user;
    }
}
